﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a plane two dimensional point.
    /// </summary>
    [Serializable]
    public class Point2D : IPoint2D
    {
        /// <summary>
        /// The x position of the point.
        /// </summary>
        private double x;

        /// <summary>
        /// The y position of the point.
        /// </summary>
        private double y;

        /// <summary>
        /// Initializes a new instance of the <see cref="Point2D"/> class.
        /// </summary>
        public Point2D()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Point2D"/> class.
        /// </summary>
        /// <param name="point">The point to clone.</param>
        public Point2D(Point2D point)
        {
            if (point == (Point2D) null)
            {
                throw new ArgumentNullException("point");
            }

            this.x = point.X;
            this.y = point.Y;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Point2D"/> class.
        /// </summary>
        /// <param name="x">The x position.</param>
        /// <param name="y">The y position.</param>
        public Point2D(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Point2D"/> class.
        /// </summary>
        /// <param name="vector">The vector, which includes the positioninformations.</param>
        public Point2D(GeneralVector2D vector)
        {
            if (vector == (GeneralVector2D) null)
            {
                throw new ArgumentNullException("vector");
            }

            this.x = vector.X;
            this.y = vector.Y;
        }

        /// <summary>
        /// Gets or sets the x position.
        /// </summary>
        /// <value>The x position.</value>
        public double X
        {
            get { return x; }
            set { x = value; }
        }

        /// <summary>
        /// Gets or sets the y position.
        /// </summary>
        /// <value>The y position.</value>
        public double Y
        {
            get { return y; }
            set { y = value; }
        }

        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance is less than <paramref name="obj"/>. Zero This instance is equal to <paramref name="obj"/>. Greater than zero This instance is greater than <paramref name="obj"/>.
        /// </returns>
        /// <exception cref="T:System.ArgumentException">
        /// 	<paramref name="obj"/> is not the same type as this instance. </exception>
        public int CompareTo(Object obj)
        {
            if (obj is Point2D)
            {
                if (this.x == ((Point2D) obj).X)
                {
                    return 0;
                }

                if (this.x < ((Point2D) obj).X)
                {
                    return -1;
                }

                if (this.x > ((Point2D) obj).X)
                {
                    return 1;
                }
            }

            return 0;
        }

        /// <summary>
        /// Adds a x and y value to the current point instance.
        /// </summary>
        /// <param name="xValue">The x value.</param>
        /// <param name="yValue">The y value.</param>
        public void Add(double xValue, double yValue)
        {
            this.x += xValue;
            this.y += yValue;
        }

        /// <summary>
        /// Inverts the position elements x and y.
        /// </summary>
        public void InvertPositionElements()
        {
            this.x = -this.x;
            this.y = -this.y;
        }

        /// <summary>
        /// Copies this current instance of the point.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public Point2D Copy()
        {
            return new Point2D(this.x, this.y);
        }

        /// <summary>
        /// Scales the position elements x and y with a specified factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor.</param>
        public void ScaleTransformation(double scaleFactor)
        {
            this.x *= scaleFactor;
            this.y *= scaleFactor;
        }

        /// <summary>
        /// Scales the position elements x and y with a specified factor.
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x position.</param>
        /// <param name="scaleFactorY">The scale factor for the y position.</param>
        public void ScaleTransformation(double scaleFactorX, double scaleFactorY)
        {
            this.x *= scaleFactorX;
            this.y *= scaleFactorY;
        }

        /// <summary>
        /// Translates the point position.
        /// </summary>
        /// <param name="translateValue">The translate value for the x- and y position.</param>
        public void TranslateTransformation(double translateValue)
        {
            this.x += translateValue;
            this.y += translateValue;
        }

        /// <summary>
        /// Translates the point position.
        /// </summary>
        /// <param name="translateValueX">The translate value for x position.</param>
        /// <param name="translateValueY">The translate value for y position.</param>
        public void TranslateTransformation(double translateValueX, double translateValueY)
        {
            this.x += translateValueX;
            this.y += translateValueY;
        }

        /// <summary>
        /// Calculates the distance from the current instance of a point to another point.
        /// </summary>
        /// <param name="point">The other point.</param>
        /// <returns>The distance from the current instance of a point to another point</returns>
        public double GetDistance(Point2D point)
        {
            if (point == (Point2D) null)
            {
                throw new ArgumentNullException("point");
            }

            Point2D tempuri = this - point;

            return ExtendedMath.Phytagoras(tempuri.X, tempuri.Y);
        }

        /// <summary>
        /// Determines whether the position elements are zero.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if the position elements are zero; otherwise, <c>false</c>.
        /// </returns>
        public bool IsZero()
        {
            return ((this.X == 0) && (this.Y == 0));
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static Point2D operator +(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            Point2D result = new Point2D();

            result.X = a.X + b.X;
            result.Y = a.Y + b.Y;

            return result;
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static Point2D operator -(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            Point2D result = new Point2D();

            result.X = a.X - b.X;
            result.Y = a.Y - b.Y;

            return result;
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static Point2D operator *(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            Point2D result = new Point2D();

            result.X = a.X * b.X;
            result.Y = a.Y * b.Y;

            return result;
        }

        /// <summary>
        /// Implements the operator /.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static Point2D operator /(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            Point2D result = new Point2D();

            result.X = a.X / b.X;
            result.Y = a.Y / b.Y;

            return result;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Point2D a, Point2D b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.X == b.X) && (a.Y == b.Y));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Point2D a, Point2D b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator &lt;.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator <(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            return ((a.X < b.X) && (a.Y < b.Y));
        }

        /// <summary>
        /// Implements the operator &lt;=.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator <=(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            return ((a.X <= b.X) && (a.Y <= b.Y));
        }

        /// <summary>
        /// Implements the operator &gt;.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator >(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            return ((a.X > b.X) && (a.Y > b.Y));
        }

        /// <summary>
        /// Implements the operator &gt;=.
        /// </summary>
        /// <param name="a">The first point.</param>
        /// <param name="b">The second point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator >=(Point2D a, Point2D b)
        {
            if (a == (Point2D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Point2D) null)
            {
                throw new ArgumentNullException("b");
            }

            return ((a.X >= b.X) && (a.Y >= b.Y));
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Transforms the current point instance into a matrix.
        /// </summary>
        /// <returns>The transformed matrix.</returns>
        public Matrix ToMatrix()
        {
            Matrix matrix = new Matrix(2, 1);

            matrix.SetValueAtPosition(new MatrixPosition(0, 0), this.x);
            matrix.SetValueAtPosition(new MatrixPosition(1, 0), this.y);

            return matrix;
        }

        /// <summary>
        /// Transforms the current point instance into a vector.
        /// </summary>
        /// <returns>The transformed vector.</returns>
        public GeneralVector ToVector()
        {
            GeneralVector vector = new GeneralVector(2);

            vector.SetComponent(0, this.x);
            vector.SetComponent(1, this.y);

            return vector;
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.x.ToString() + "; " + this.y.ToString();
        }
    }
}